//
//  EPMShoppingCarViewController.m
//  myXianfeng
//
//  Created by 马继鵬 on 17/4/26.
//  Copyright © 2017年 EPM. All rights reserved.
//

#import "EPMShoppingCarViewController.h"

#import "EPMDestinationTableViewCell.h"
#import "EPMAddressViewController.h"
#import "EPMLightTableViewCell.h"
#import "EPMTimeTableViewCell.h"
#import "EPMCouTableViewCell.h"
#import "EPMGoodsTableViewCell.h"
#import "EPMSummaryTableViewCell.h"


@interface EPMShoppingCarViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak,nonatomic)  UITableView* normalView;

@property (weak,nonatomic)  UIView* emptyView;

@property (assign,nonatomic) NSArray* goodList;

//@property (weak,nonatomic)  UITableView* tabelView;
@end

@implementation EPMShoppingCarViewController
- (void)viewDidLoad{
    [super viewDidLoad];
    
    self.navigationItem.title = @"购物车";
     [self setupUI];
     [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
       
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
   

}

#pragma mark - 设置UI -
- (void)setupUI{
    BOOL isEmpty = _ShopCar.isEmpty;
     [self setEmptyView];
     [self setNormalView];
    if ( isEmpty){
       
        self.normalView.hidden = YES;
        self.emptyView.hidden = NO;
        
    }else{
        self.normalView.hidden = NO;
        self.emptyView.hidden = NO;
    }
    self.goodList = _ShopCar.goodsList;
     //接收通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shopCarGoodListChangedNotification:) name:EPMShopCarGoodListChangedNotification object:nil];
}
- (void)shopCarGoodListChangedNotification:(NSNotification*)noti{
    NSArray* list = noti.userInfo[KShopCarGoodList];
    self.goodList = list;
    [self.normalView reloadData];
    
    //监听购物车是否为空,并完成变换
    BOOL isEmpty = _ShopCar.isEmpty;
    if ( isEmpty){
        
        self.normalView.hidden = YES;
        self.emptyView.hidden = NO;
        
    }else{
        self.normalView.hidden = NO;
        self.emptyView.hidden = NO;
    }
    
}

#pragma mark - 设置购物车为空 -

- (void)setEmptyView{
    UIView* emptyView = [[UIView alloc]init];
    self.emptyView = emptyView;
    [self.view addSubview:emptyView];
    
    [emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    
    UIImageView* carView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"shopping_car"]];
    
    [emptyView addSubview:carView];
   
    [carView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(emptyView);
        make.bottom.equalTo(emptyView.mas_centerY).offset(-30);
        make.width.height.mas_equalTo(100);
    }];
    
    UILabel* desLabel = [[UILabel alloc]init];
    [emptyView addSubview:desLabel];
    [desLabel sizeToFit];
    desLabel.text = @"亲购物车是空的耶~赶紧去挑点好吃的吧";
    desLabel.textColor = [UIColor darkGrayColor];
    desLabel.textAlignment = NSTextAlignmentCenter;
    
    [desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(carView);
        make.top.equalTo(carView.mas_bottom).offset(37);
    }];
    
    UIButton* btn = [[UIButton alloc]init];
    [btn setTitle:@"去逛逛" forState:UIControlStateNormal];
    [btn setTitleColor:appColor forState:UIControlStateNormal];
    [btn.layer setBorderWidth:1.0];
    [btn.layer setBorderColor:appColor.CGColor];
    [btn.layer setMasksToBounds:YES];
    [btn.layer setCornerRadius:5.0];
    
    [emptyView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(desLabel);
        make.top.equalTo(desLabel.mas_bottom).offset(17);
        make.height.mas_equalTo(30);
        make.width.equalTo(desLabel).multipliedBy(2.0/3);
    }];
    
    [btn addTarget:self action:@selector(actionGoHomePage:) forControlEvents:UIControlEventTouchUpInside];
    
}
#pragma mark - 点击跳转到首页方法 -

- (void)actionGoHomePage:(UIButton*)btn{
    self.tabBarController.selectedIndex = 0;
}

#pragma mark - 设置购物车 -

- (void)setNormalView{
    
    UITableView* normalView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.view addSubview:normalView];
    self.normalView = normalView;
    
    [normalView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    
    normalView.delegate = self;
    normalView.dataSource = self;
    normalView.estimatedRowHeight = 50;
    
    [normalView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [normalView registerNib:[UINib nibWithNibName:@"EPMDestinationTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"address"];
    [normalView registerNib:[UINib nibWithNibName:@"EPMLightTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"light"];
    [normalView registerNib:[UINib nibWithNibName:@"EPMTimeTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"time"];
    [normalView registerNib:[UINib nibWithNibName:@"EPMCouTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cou"];
    
    [normalView registerNib:[UINib nibWithNibName:@"EPMGoodsTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"goods"];
    [normalView registerNib:[UINib nibWithNibName:@"EPMSummaryTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"summary"];
}

#pragma mark - 数据源方法 -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0){
        return 1;
    }else{
        return self.goodList.count + 4;
    }
    
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //第0组
    if (indexPath.section == 0){
        EPMDestinationTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"address" forIndexPath:indexPath];
        [cell setAddressAlterBlock:^{
            EPMAddressViewController* VC = [EPMAddressViewController new];
            [self.navigationController pushViewController:VC animated:YES];
        }];
        
        return cell;
    }
    
    //第1组第0行
    if (indexPath.section == 1 && indexPath.row == 0){
        EPMLightTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"light" forIndexPath:indexPath];
        return cell;
    }
    
    //第1组第1行
    
    if (indexPath.section == 1 && indexPath.row == 1){
        EPMTimeTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"time" forIndexPath:indexPath];
        return cell;

    }
    
    //第1组第2行
    if (indexPath.section == 1 && indexPath.row == 2){
        EPMCouTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cou" forIndexPath:indexPath];
        return cell;

    }
    //第1组最后一行
    if (indexPath.section == 1 && indexPath.row == self.goodList.count + 3){
        EPMSummaryTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"summary" forIndexPath:indexPath];
        return cell;

    }
    
    
    EPMGoodsTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"goods" forIndexPath:indexPath];
    cell.model = self.goodList[indexPath.row - 3];
    
    return cell;
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
